<p class="Head1"><help:link Id="66464">Variables</help:link><help:key-word value="names of variables" tag="kw66464_1"/><help:key-word value="variables" tag="kw66464_3"/></p>
<p class="Paragraph">The following describes some of the most common variables.</p>
</help:to-be-embedded>
<p class="Paragraph">Variables are an essential component of Basic programs. Variables store numeric values, strings or objects at runtime. Variables can be read by the program or the contents can be modified. In <help:productname xmlns:help="http://openoffice.org/2000/help">%PRODUCTNAME</help:productname> Basic, you don't need to declare variables explicitly. As soon as a variable is used the first time, it is declared automatically. The statement <span class="T1">Option Explicit</span> can be used to force a variable-declaration with the <span class="T1">Dim</span> statement.</p>
<p class="Head2">Naming Conventions for Variables</p>
<p class="Paragraph">A variable name can consist of a maximum of 255 characters. The first character of a variable name must be a letter of the alphabet. Numbers can also be used in a variable name, but punctuation symbols and special characters are not permitted, with exception of the underscore character ("_"). <help:productname xmlns:help="http://openoffice.org/2000/help">%PRODUCTNAME</help:productname> Basic does not distinguish between upper and lower-case letters. Variable names with spaces must be enclosed in brackets.</p>
<p class="TextInTable">Not valid, punctuation marks not allowed</p>
</span></td></tr></table>
<p class="Head2">Variable Declarations</p>
<p class="Paragraph">In <help:productname xmlns:help="http://openoffice.org/2000/help">%PRODUCTNAME</help:productname> Basic, as opposed to other programming languages, you don't need to declare variables explicitly. An variable declaration can be performed with the <span class="T1">Dim</span> statement. You can declare more than one variable at a time by separating the names with a comma. To define the variable type, use either a type-declaration sign after the name, or the appropriate key word. Some examples:</p>
<p class="TextInTable">Declares one variable as a String and one as an Integer, separated by comma</p>
</span></td></tr></table>
<p class="Paragraph">It is very important when declaring variables that you use the type-declaration character each time, even if it was used in the declaration instead of a keyword. Thus the following statements are invalid:</p>
<p class="TextInTable">Once you have declared a variable as a certain type, you cannot declare the variable under the same name again as a different type!</p>
<p class="Paragraph">To force every variable to be declared, use the following command:</p>
<p class="PropText">OPTION EXPLICIT</p>
<p class="Paragraph">The Option Explicit statement has to be the first line in the source code of a module, i.e., before the first SUB. Generally, only Arrays need to be declared explicitly. All other variables are declared according to the type-declaration character, or - if omitted - as the default type <span class="T1">Single</span>. One advantage of forcing variable declaration is the prevention of spelling mistakes. Here's an example of a forced declation:</p>
<p class="Paragraph">Assume the variable StorageMedium$ has been used in a SUB. At some point in the program code, you assign a specific event to the variable with the following (misspelled) command:</p>
<p class="Paragraph"><help:productname xmlns:help="http://openoffice.org/2000/help">%PRODUCTNAME</help:productname> Basic would obviously accept this expression as a new variable, since variables usually don't need to be declared. If you used <span class="T1">Option Explicit</span> however, StarBasic would not recognize the (incorrect) variable name and would report an error when you run the program.</p>
<p class="Head2"><help:key-word value="types of variables" tag="kw66464_2" xmlns:help="http://openoffice.org/2000/help"/>Types of Variables</p>
<p class="Paragraph"><help:productname xmlns:help="http://openoffice.org/2000/help">%PRODUCTNAME</help:productname> Basic supports a number of variable types. Variables can generally be categorized in the four following types:</p>
<p class="Paragraph"><span class="T1">Numeric</span> variables can contain number values in certain range limits. Certain variables are used to store large or small numbers, and others for floating-point or fractional numbers. Each of the variable types has advantages and disadvantages. For instance, integer variables do not require much memory and thus are rapidly calculated. Floating-point numbers, however, require more memory and time (for calculation), but are more precise.</p>
<p class="Paragraph"><span class="T1">String</span> variables contain strings of any characters.</p>
<p class="Paragraph"><span class="T1">Boolean</span> variables can only indicate the status TRUE or FALSE.</p>
<p class="Paragraph"><span class="T1">Object</span> variables can store objects of various types. Objects, for instance, can refer to tables and documents within a document. You can use the corresponding methods and properties in the program to "edit" these objects.</p>
<p class="Head3">Integer Variables</p>
<p class="Paragraph">Integer variables range from <text:s text:c="" xmlns:text="http://openoffice.org/2000/text"/>-32768 to 32767. If you assign a floating-point value to an integer variable, the decimal places are truncated. Integer variables are rapidly calculated in procedures and are suitable for counter variables in loops. Internally, an integer variable only requires 2 bytes of memory. <text:s text:c="" xmlns:text="http://openoffice.org/2000/text"/>"%" is the type-declaration character.</p>
<p class="Paragraph">Declaration:</p>
<p class="PropText">Dim Variable%</p>
<p class="PropText">Dim Variable As Integer</p>
<p class="Head3">Long Integer Variables</p>
<p class="Paragraph">Long integers can hold whole numbers between -2,147,483,648 and 2,147,483,647. If you assign a floating-point value to an integer variable, the decimal places are truncated. Long integer variables are rapidly calculated in procedures and are suitable for counter variables in loops for large values. Internally, a long integer variable requires 4 bytes of memory. <text:s text:c="" xmlns:text="http://openoffice.org/2000/text"/>"&" is the type-declaration character.</p>
<p class="Paragraph">Declaration:</p>
<p class="PropText">Dim Variable&</p>
<p class="PropText">Dim Variable as Long</p>
<p class="Head3">Single Variables</p>
<p class="Paragraph">Single variables can hold positive or negative values from 3,402823 x 10E38 to 1,401298 x 10E-45. Single variables are floating-point variables. A characteristic of these numbers is that the decimal precision decreases as the non-decimal part of the number increases. <text:s text:c="" xmlns:text="http://openoffice.org/2000/text"/>Single variables are suitable for mathematical calculations of average precision. Calculations require more time than for Integer variables, but are faster than calculations with Double variables. Internally, a Single variable requires 4 bytes of memory. The type-declaration character is <text:s text:c="" xmlns:text="http://openoffice.org/2000/text"/>"!".</p>
<p class="PropText">Dim Variable!</p>
<p class="PropText">Dim Variable as Single</p>
<p class="Head3">Double Variables</p>
<p class="Paragraph">Double variables can hold positive or negative values from 1,79769313486232 x 10E308 to 4,94065645841247 x 10E-324. Double variables are floating-point variables, in which the decimal precision decreases as the non-decimal part of the number increases. Double variables are suitable for precise calculations and require 8 bytes of memory. The type-declaration character is <text:s text:c="" xmlns:text="http://openoffice.org/2000/text"/>"#".</p>
<p class="PropText">Dim Variable#</p>
<p class="PropText">Dim Variable As Double</p>
<p class="Head3">Currency Variables</p>
<p class="Paragraph">Currency variables are stored internally as 64-bit numbers (8 Bytes), displayed in integer format and divided by 10,000. This results in a fixed-decimal format with 15 non-decimal and 4 decimal places. The values must lie in the range from -922.337.203.685.477,5808 to +922.337.203.685.477,5807. Currency variables are used to calculate currency values with a relatively high precision. The <text:s text:c="" xmlns:text="http://openoffice.org/2000/text"/>type-declaration character is "@".</p>
<p class="PropText">Dim Variable@</p>
<p class="PropText">Dim Variable As Currency</p>
<p class="Head3">String Variables</p>
<p class="Paragraph">String variables can hold any character string with up to 64,000 characters. Each character is stored as a 1-byte ASCII character. String variables are suitable for word processing within programs and for temporary storage of any non-printable character up to a maximum length of 64 Kbytes. The memory required for storing string variables depends on the number of characters in the variable. The type-declaration character is "$".</p>
<p class="PropText">Dim Variable$</p>
<p class="PropText">Dim Variable As String</p>
<p class="Paragraph"/>
<p class="Head3">Boolean Variables</p>
<p class="Paragraph">Boolean variables store only one of two values: TRUE or FALSE. If you use the <span class="T1">Print</span> command to display the content of a Boolean variable, True is returned as "-1", False as "0". Booleans are used to store binary values, e.g., a result of a comparison. Internally, a 2-byte integer value is stored. Any value assigned to a Boolean is converted to "False" if the value is not exactly equal to "-1". Boolean variables can only be declared by the key words True or False.</p>
<p class="PropText">Dim Variable As Boolean</p>
<p class="Head3">Date Variables</p>
<p class="Paragraph">Date variables can only contain dates and time values stored in an internal format. Values assigned to Date variables with <span class="T1">Dateserial</span>, <span class="T1">Datevalue</span>, <span class="T1">Timeserial</span> or <span class="T1">Timevalue</span> are automatically converted to the internal format. Date-variables are converted to normal numbers by using the <span class="T1">Day</span>, <span class="T1">Month</span>, <span class="T1">Year</span> or the <span class="T1">Hour</span>, <span class="T1">Minute</span>, <span class="T1">Second</span> function. The internal format enables a comparison of date/time values by calculating the difference between two numbers. These variables can only be declared with the key word <span class="T1">Date</span>.</p>
<p class="PropText">Dim Variable As Date</p>
<p class="Head2">Defining Variables</p>
<p class="Paragraph">As soon as the variable has been declared, it is automatically set to the "Null" value, i.e., you do not need to manually reset a declared variable to a zero-value. Note the following conventions:</p>
<p class="Paragraph"><span class="T1">Numeric</span> variables are automatically assigned the value "0" as soon as they are declared.</p>
<p class="Paragraph"><span class="T1">Date variables</span> are assigned the value 0 internally; equivalent to converting the value to <text:s text:c="" xmlns:text="http://openoffice.org/2000/text"/>"0" with the <span class="T1">Day</span>, <span class="T1">Month</span>, <text:s text:c="" xmlns:text="http://openoffice.org/2000/text"/><span class="T1">Year</span> or <span class="T1">Hour</span>, <span class="T1">Minute</span>, <span class="T1">Second</span> function.</p>
<p class="Paragraph"><span class="T1">String variables</span> are assigned an empty-string ("") when they are declared.</p>
<p class="Head2">Predefined system variables</p>
<p class="Paragraph"><help:productname xmlns:help="http://openoffice.org/2000/help">%PRODUCTNAME</help:productname> Basic supports several predefined system variables that always return a certain result in a query:</p>
<p class="Head3">TRUE and FALSE</p>
<p class="Paragraph">These are Boolean variables and return either the value 0 (FALSE) or -1 (TRUE). These variables can be used, for example, in <span class="T1">If...Then...Else</span> queries to test the logical value of a comparison. Comparisons are represented by the comparison operators <text:s text:c="" xmlns:text="http://openoffice.org/2000/text"/>(=, <, >) The statement:</p>
<p class="PropText">PRINT 5=3</p>
<p class="Paragraph">returns the value 0 (FALSE), since the expression 5=3 is false. The statement:</p>
<p class="PropText">PRINT 5>3</p>
<p class="Paragraph">returns the value -1 (TRUE), since the expression is true. The use of system variables doesn't technically alter the actual numeric values of 0 or -1, but the use of TRUE / FALSE variables improves the readability of program code.</p>
<p class="Head3">PI</p>
<p class="Paragraph">PI is a mathematical constant that returns the infinite number Pi = 3,141592657...</p>
<p class="Head2">Arrays</p>
<p class="Paragraph"><help:productname xmlns:help="http://openoffice.org/2000/help">%PRODUCTNAME</help:productname> Basic uses single or multiple arrays, defined by a specified variable type. Arrays are suitable for editing lists and tables in programs. The advantage of an array is that the individual elements can be addressed via an index of numeric expressions or variables.</p>
<p class="Paragraph">Arrays are declared with the <span class="T1">Dim</span> statement. There are two ways to define how an index can be addressed:</p>
<p class="PropText">33 elements, Dimension 0, from 0 to 10, <text:s text:c="" xmlns:text="http://openoffice.org/2000/text"/>Dimension 1, from 0 to 10, Dimension 2, from 0 to 10.</p>
<p class="PropText">21 elements (including 0), numbered from -15 to 5</p>
</span></td></tr></table>
<p class="Paragraph">The index range can include positive as well as negative numbers. The maximum number of elements that can be addressed via an index is 16,368.</p>
<p class="Head2">Constants</p>
<p class="Paragraph">Constants are a "variant" of variables that aid in making program code easier to read. Constants are not defined as a certain type, but are used instead as a placeholder in the program code. A constant can only be defined once in the program and cannot be redefined. The following command is used to define a constant:</p>
<p class="Paragraph">The kind of an expression specified in the command line is not taken into account. As soon as a program is started, the program code is converted internally in <help:productname xmlns:help="http://openoffice.org/2000/help">%PRODUCTNAME</help:productname> Basic and each constant is replaced with the predefined expression, regardless of constant type.</p>